<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Response;
use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
//引用对应的命名空间
use Captcha;
use Session;

class LoginController extends AdminController
{   
    /**
     * 后台登录
     * @author yhs 2019.07.25
     * @return data
     */
    public function login(Request $request){
        // $captcha = $request->input('capcha');//dd( Session::all());die;
        // // 获取验证码
        // $milkcaptcha = Session::get('milkcaptcha_home');
        // dd($request->input('key'));die;
        if (captcha_api_check($request->input('capcha'), $request->input('key'))){
            // $users = DB::table('admin')->get();dd($users);die;
            $credentials = $request->only('username', 'password');//bcrypt('123456')加密模式
            if (Auth::guard('admin')->attempt($credentials)) {
                $model = Auth::guard('admin')->user();
                $model->count = $model->count + 1; 
                $model->save();
                // 身份验证通过...
                $token = md5(rand(99999,9999999));
                $data = [
                    'username'=>Auth::guard('admin')->user()->username,
                    'uid'=>Auth::guard('admin')->user()->id,
                    'token'=>$token,
                    'status'=>1,
                    'msg'=>'登录成功！'
                ];
                return $data;
                //return redirect()->intended('/admin/index');跳转页面
            }else{
                $data = ['status'=>0,'msg'=>'用户名或密码有误！'];
                return $data;
            }
        }else{
            //return back()->with('errors', '验证码有误！');
            $data = ['status'=>0,'msg'=>'验证码有误！'];
            return json_encode($data);
        }
    }
    /**
     * 生成验证码
     * @author yhs 2019.07.25
     */
    public function captcha(Request $request)
    {
                //生成验证码图片的Builder对象，配置相应属性
        // $builder = new CaptchaBuilder;
        // //可以设置图片宽高及字体
        // $builder->build($width = 100, $height = 40, $font = null);
        // //获取验证码的内容
        // $phrase = $builder->getPhrase();
        
        // //把内容存入session
        // Session::flash('milkcaptcha_home', $phrase);//dd(Session::all());die;
        // //生成图片
        // header("Cache-Control: no-cache, must-revalidate");
        // header('Content-Type: image/jpeg');
        // $builder->output();
        return json_encode(app('captcha')->create('default', true));

        
    }
    /**
     * 退出登录
     * @author yhs 2019.07.25
     * @return array
     */    
    public function logout(){
        Auth::guard('admin')->logout();
        return ['status'=>1,'message'=>"退出成功"];
    }

    /**
     * api添加
     * @author yhs 2019.02.26
     * @return list
     */
    public function apiadd(Request $request){
        
        $data = [
            'text' => addslashes($request->text),
        ];
        try{
            if(empty($request->id)){
                DB::table('text')->insert($data);
                $back['message'] = "添加成功！";
                $back['status'] = 1;
            }else{
                Menu::where('id',$request->id)->update($data);
                $back['message'] = "修改成功！";
                $back['status'] = 1;
            }
        }catch(\Execption $e){
            $back['message'] = $e->getMessage();
            $back['status'] = 0;
        }
        return $back;
    }
    public function apilist(Request $request){
        if(!empty($request->id)){
            $find = DB::table('text')->where('id',$request->id)->first();
            $find->text = stripslashes($find->text);
        }else{
            $find = [];
        }
        return json_encode($find);
    }
}
